/*
 * Copyright 2011 Todd Bealmear
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dd0t.android.fbct;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * Management class for OverlayItem's used in the MapActivity
 * @version 1.0.0
 * @author Todd Bealmear, <a href="http://dd0t.com">dd0t.com</a>
 */
public class FBCTMarkerOverlay extends ItemizedOverlay<OverlayItem> {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	
	/**
	 * Constructor for FBCTMarkerOverlay
	 * @param marker Drawable element used as a marker on the map
	 */
	public FBCTMarkerOverlay(Drawable marker) {
		super(boundCenterBottom(marker));
		populate();
	}
	
	/**
	 * Retrieves an OverlayItem
	 * @param arg0 int index of the OverlayItem
	 * @return The OverlayItem at the index
	 */
	@Override
	protected OverlayItem createItem(int arg0) {
		return mOverlays.get(arg0);
	}

	/**
	 * Retrieves the size of the OverlayItem list
	 * @return int representation of the OverlayItems list size
	 */
	@Override
	public int size() {
		return mOverlays.size();
	}
	
	/**
	 * Callback for marker taps
	 * @param index The index of the OverlayItems list
	 * @return true if the tap was handled correctly, false otherwise
	 */
	@Override
	protected boolean onTap(int index) {
		AlertDialog.Builder dialog = new AlertDialog.Builder(FBCheckinThingy.mContext);
		dialog.setTitle(mOverlays.get(index).getTitle());
		dialog.setMessage(mOverlays.get(index).getSnippet());
		dialog.setNeutralButton("Close", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		dialog.show();
		return super.onTap(index);
	}
	
	/**
	 * Adds an OverlayItem to the list
	 * @param item The OverlayItem to add
	 */
	public void add(OverlayItem item) {
		mOverlays.add(item);
		populate();
	}

}
